package org.mspring.mlog.web.module.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mspring.mlog.entity.Album;
import org.mspring.mlog.entity.constants.PageNames;
import org.mspring.mlog.service.AlbumService;
import org.mspring.mlog.utils.PermissionUtils;
import org.mspring.platform.config.ConfigService;
import org.mspring.platform.dao.Order.OrderType;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.FreemarkerVariableNames;
import org.mspring.platform.web.freemarker.widget.stereotype.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 相册
 *
 * @author Gao Youbo
 * @since 2012-11-23
 */
@Action
@RequestMapping("/album")
public class Web_AlbumWidget extends Web_AbstractWidget {

    @Autowired
    private ConfigService configService;
    @Autowired
    private AlbumService albumService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Album> albumPage = new Page<>();
        albumPage.setPageNo(page);
        if (albumPage.getOrder() == null || albumPage.getOrder().isEmpty()) {
            albumPage.addOrder("sortOrder", OrderType.DESC);
            albumPage.addOrder("createTime", OrderType.DESC);
        }
        String pageSize = configService.getConfig("web_albumlist_size");
        if (StringUtils.isNotBlank(pageSize)) {
            albumPage.setPageSize(Integer.parseInt(pageSize));
        }
        QueryBuilder queryBuilder = QueryBuilder.instance(Album.class).buildValuedNotEqual("type", Album.Type.PRIVATE);
        albumService.listPage(queryBuilder, albumPage);
        model.addAttribute(FreemarkerVariableNames.ALBUM_PAGE, albumPage);
        setCurrnetPage(model, PageNames.ALBUM);
        return "skin:/album";
    }

    /**
     * 相册授权
     *
     * @param albumId
     * @param password
     * @param request
     * @param response
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/token")
    public String albumToken(@RequestParam(required = false) Long albumId, @RequestParam(required = false) String password, HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
        boolean has_permission = PermissionUtils.hasAlbumPermission(albumId, password);
        if (!has_permission) {
            model.addAttribute("albumId", albumId);
            model.addAttribute("not_token", false);
            return "skin:/album-token";
        }
        PermissionUtils.setAlbumPermission(albumId, password, session);
        return String.format("redirect:/photo/list-%s.html", albumId);
    }

}
